<!--
 * @Date: 2020-04-22 14:03:56
 * @LastEditors: 黄栋
 * @LastEditTime: 2020-04-22 15:35:47
 * @FilePath: /cloudAdmin/src/views/market/promotion/member/components/singSet.vue
 -->
<template>
  <!--统一的编辑和新增 -->
  <el-dialog width="700px" :close-on-click-modal="false" :visible.sync="visible" title="签到送积分">
    <el-form label-width="130px" size="small">
      <el-form-item label="每日签到积分">
        <el-input v-model="list1val"></el-input>
      </el-form-item>
      <el-form-item label="连续签到奖励">
        <el-radio-group v-model="list2flagl">
          <el-radio :label="1">开启</el-radio>
          <el-radio :label="2">暂不开启</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-card v-if="list2flagl==1">
        <el-form label-width="100px" size="mini" inline>
          <el-form-item label="连续签到天数">
            <el-input v-model="list2[0].continuity_num"></el-input>
          </el-form-item>
          <el-form-item label="奖励积分">
            <el-input v-model="list2[0].score"></el-input>
          </el-form-item>
        </el-form>
        <span>说明：连续签到n天后，第n天开始往后每天奖励y个积分。连续间断（且未补签）则连续天数再从头开始</span>
      </el-card>
      <el-form-item label="累计签到奖励">
        <el-radio-group v-model="list3flagl">
          <el-radio :label="1">开启</el-radio>
          <el-radio :label="2">暂不开启</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-card v-if="list3flagl==1">
        <el-form size="small" label-width="100px" inline>
          <div v-for="(item,index) in list3" :key="index">
            <el-form-item label="累计签到天数">
              <el-input style="width:150px" v-model="item.total_num"></el-input>
            </el-form-item>
            <el-form-item label="奖励积分">
              <el-input style="width:150px" v-model="item.score"></el-input>
            </el-form-item>
            <el-button style="float:right" size="small" @click="list3.splice(index,1 )">删除</el-button>
          </div>
          <el-button
            size="small"
            @click="list3.push({total_num:'',type:3,score:'',status:list3flagl})"
          >添加</el-button>
        </el-form>
      </el-card>
      <el-form-item label="特殊节日签到奖励">
        <el-radio-group v-model="list4flagl">
          <el-radio :label="1">开启</el-radio>
          <el-radio :label="2">暂不开启</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-card v-if="list4flagl==1">
        <el-form size="small" label-width="100px" inline>
          <div v-for="(item,index) in list4" :key="index">
            <el-form-item label="特殊日期">
              <el-date-picker
                style="width:150px"
                v-model="item.special_date"
                value-format="MM-dd"
                type="date"
                placeholder="选择日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="奖励积分">
              <el-input style="width:150px" v-model="item.score"></el-input>
            </el-form-item>
            <el-button style="float:right" size="small" @click="list4.splice(index,1 )">删除</el-button>
          </div>
          <el-button
            size="small"
            @click="list4.push({special_date:'',type:4,score:'',status:list4flagl})"
          >添加</el-button>
        </el-form>
      </el-card>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">取消</el-button>
      <el-button size="small" type="primary" @click="editFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>
 
 <script>
export default {
  data() {
    return {
      visible: false,
      list1: [{ status: 1, type: 1, score: 0 }],
      list2: [
        {
          continuity_num: "",
          score: "",
          type: 2
        }
      ],
      list3: [{ type: 3 }],
      list4: [{ type: 4 }],
      list1val: 0,
      list2flagl: 2,
      list3flagl: 2,
      list4flagl: 2,
      editForm: { id: "" }
    };
  },
  watch: {
    list1val(oval, nval) {
      this.list1[0].score = this.list1val;
    },
    list2flagl() {
      this.list2[0].status = this.list2flagl;
    },
    list3flagl() {
      this.list3.forEach(item => {
        this.$set(item, "status", this.list3flagl);
      });
    },
    list4flagl() {
      this.list4.forEach(item => {
        this.$set(item, "status", this.list4flagl);
      });
    }
  },
  methods: {
    /**初始化数据 */
    initData(id) {
      this.getSingSet(1);
      this.getSingSet(2);
      this.getSingSet(3);
      this.getSingSet(4);
      this.visible = true;
    },
    getSingSet(type) {
      this.$http({
        url: this.$http.adornUrl("/market/score/get_sign_set"),
        method: "post",
        data: this.$http.adornData({ type: type })
      }).then(res => {
        if (res.code == 200) {
          if (type == 1 && res.data.length > 0) {
            this.list1 = res.data;
            this.list1val = res.data[0].score;
          } else if (type == 2 && res.data.length > 0) {
            this.list2 = res.data;
            this.list2flagl = res.data[0].status;
          } else if (type == 3 && res.data.length > 0) {
            this.list3 = res.data;
            this.list3flagl = res.data[0].status;
          } else if (type == 4 && res.data.length > 0) {
            this.list4 = res.data;
            this.list4flagl = res.data[0].status;
          }
        } else {
          this.$message.error(res.message);
        }
      });
    },
    /**新增/编辑 数据 */
    editFormSubmit() {
      var nums = this.list1.concat(this.list2, this.list3, this.list4);
      this.$http({
        url: this.$http.adornUrl("/market/score/edit_sign_set"),
        method: "post",
        data: this.$http.adornData({ list: JSON.stringify(nums) })
      }).then(res => {
        if (res.code == 200) {
          this.$message({
            message: res.message,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
            }
          });
        } else {
          this.$message.error(res.message);
        }
      });
    }
  }
};
</script>
 
 <style>
</style>
